// Simple Dialog Box Plugin by Dharla Ferdana
// URL: http://www.dhf.web.id
// Licence: none
var uniqueId = new Date().getTime();
(function () {
    var b = document.createElement("div"),
        a = document.createElement("div");
    b.className = "dialog-box";
    b.id = "dialog-box-" + uniqueId;
    b.innerHTML = '<h3 class="dialog-title">&nbsp;</h3><a href="javascript:;" class="dialog-minmax" title="Minimize">&ndash;</a><a href="javascript:;" class="dialog-close" title="Close">&times;</a><div class="dialog-content">&nbsp;</div><div class="dialog-action"></div>';
    a.className = "dialog-box-overlay";
    document.body.appendChild(b);
    document.body.appendChild(a)
})();
var maximize = false,
    dialog = document.getElementById("dialog-box-" + uniqueId),
    dialog_title = dialog.children[0],
    dialog_minmax = dialog.children[1],
    dialog_close = dialog.children[2],
    dialog_content = dialog.children[3],
    dialog_action = dialog.children[4],
    dialog_overlay = dialog.nextSibling;

function setDialog(p, d) {
    var g = null,
        l = 0,
        k = 0,
        n = 0,
        a = 0,
        e = {
            title: dialog_title.innerHTML,
            content: dialog_content.innerHTML,
            width: 300,
            height: 150,
            top: false,
            left: false,
            buttons: {
                Close: function () {
                    setDialog("close")
                }
            },
            specialClass: "",
            fixed: true,
            overlay: false
        };
    for (var h in d) {
        e[h] = (typeof (d[h])) ? d[h] : e[h]
    }

    function o(i) {
        g = i;
        n = l - g.offsetLeft;
        a = k - g.offsetTop
    }

    function c(i) {
        l = document.all ? window.event.clientX : i.pageX;
        k = document.all ? window.event.clientY : i.pageY;
        if (g !== null) {
            g.style.left = !e.left ? ((l - n) + g.offsetWidth / 2) + "px" : ((l - n) - e.left) + "px";
            g.style.top = !e.top ? ((k - a) + g.offsetHeight / 2) + "px" : ((k - a) - e.top) + "px"
        }
    }

    function m() {
        g = null
    }
    dialog.className = "dialog-box " + (e.fixed ? "fixed-dialog-box " : "") + e.specialClass;
    dialog.style.visibility = (p == "open") ? "visible" : "hidden";
    dialog.style.opacity = (p == "open") ? 1 : 0;
    dialog.style.width = e.width + "px";
    dialog.style.height = e.height + "px";
    dialog.style.top = (!e.top) ? "50%" : "0px";
    dialog.style.left = (!e.left) ? "50%" : "0px";
    dialog.style.marginTop = (!e.top) ? "-" + e.height / 2 + "px" : e.top + "px";
    dialog.style.marginLeft = (!e.left) ? "-" + e.width / 2 + "px" : e.left + "px";
    dialog_title.innerHTML = e.title;
    dialog_content.innerHTML = e.content;
    dialog_action.innerHTML = "";
    dialog_overlay.style.display = (p == "open" && e.overlay) ? "block" : "none";
    if (e.buttons) {
        for (var f in e.buttons) {
            var b = document.createElement("a");
            b.className = "btn";
            b.href = "javascript:;";
            b.innerHTML = f;
            b.onclick = e.buttons[f];
            dialog_action.appendChild(b)
        }
    } else {
        dialog_action.innerHTML = "&nbsp;"
    }
    dialog_title.onmousedown = function () {
        o(this.parentNode);
        return false
    };
    dialog_minmax.innerHTML = "&ndash;";
    dialog_minmax.title = "Minimize";
    dialog_minmax.onclick = dialogMinMax;
    dialog_close.onclick = setDialog;
    document.onmousemove = c;
    document.onmouseup = m;
    maximize = (p == "open") ? true : false
}

function dialogMinMax() {
    if (maximize) {
        dialog.className += " minimize";
        dialog_minmax.innerHTML = "+";
        dialog_minmax.title = dialog_title.innerHTML.replace(/<.*?>/g, "");
        maximize = false
    } else {
        dialog.className = dialog.className.replace(/(^| )minimize($| )/g, "");
        dialog_minmax.innerHTML = "&ndash;";
        dialog_minmax.title = "Minimize";
        maximize = true
    }
};
